﻿using System;
using System.Xml;
using System.Collections;
using System.Collections.Generic;

namespace Parsers 
{
    #region OldCode
    //public enum CommandType : int
    //{
    //    RemoteConsole,
    //    RemoteDesktop,
    //    Chat,
    //    Unknown
    //}; 
    #endregion

    /// <summary>
    ///Command Desription object to pass the command & request info to processor
    /// </summary>
    public class CommandDescriptor
    {
        private string commandType;     // The command to execute
        private string commandArgs;     // The command arg to provide
        private System.Collections.Generic.KeyValuePair<string, string>[] args; //The input to command
        private string data;    //associated data

        public CommandDescriptor(string commandType, KeyValuePair<string,string>[] args, string data)
        {
            this.commandType = commandType;
            this.args = args;
            this.data = data;
        }

        public CommandDescriptor(string commandType, string[] arrayArgs, string data)
        {
            this.commandType = commandType;
            this.data = data;

            // populate args with keys as 0,1,2,3.. and corresponding values
            this.args = new KeyValuePair<string,string>[arrayArgs.Length];
            for (int i = 0; i < arrayArgs.Length; i++)
            {
                this.args[i] = new KeyValuePair<string, string>(i.ToString(), arrayArgs[i]);
            }
        }

        public string Command
        {
          get { return commandType; }
        }

        public string CommandArgs
        {
            get { return commandArgs; }
            set
            {
                commandArgs = value;
            }
        }

        public KeyValuePair<string,string>[] ArgList
        {
          get { return args; }
        }

        public string InnerData
        {
          get { return data; }
        }

    }

    interface IParser
    {
        CommandDescriptor Parse(string XMLToParse);
    }

    public abstract class Parser : IParser
    {
        /// <summary>
        ///Command Name - The name of command for which parser is defined 
        /// </summary>
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
        }

        public Parser(string name)
        {
            this.name = name;
        }

        public abstract CommandDescriptor Parse(string XMLToParse);

        // Parses a simple non-nested JSON {key:value; key2:value2; ....keyN: valueN;}
        public static KeyValuePair<string,string> [] ParseJSON(string JSONString)
        {
            JSONString = JSONString.Trim('{', '}','\r','\n','\t');
            string[] KeyValue = JSONString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            KeyValuePair<string,string>[] data = new KeyValuePair<string,string>[KeyValue.Length];
            for (int i = 0; i < KeyValue.Length; i++)
			{
                string[] extracts = KeyValue[i].Split(':');
                string key = extracts[0].Trim('\r', '\n', '\t');
                string value = extracts[1].Trim('\r', '\n', '\t');
                key = key.TrimStart();
                value = value.TrimEnd();
                data[i] = new KeyValuePair<string, string>(extracts[0].Trim('\r', '\n', '\t'), extracts[1].Trim('\r', '\n', '\t'));
			}
            return data;
        }
    }

    //ToDo: Add file transfer parsers also
}
